package blackstar.plugin.performance.stat;

/**
 * @author RayWoo (ayufox@gmail.com)
 * 
 */
public class ThreadHoldConfig
{
	private long[] threadholds; //以纳秒为单位存储

	//参数threadholds以毫秒为单位
	public ThreadHoldConfig(String threadholds)
	{
		if (threadholds.trim().length() == 0)
		{
			throw new IllegalArgumentException("Illegal threadholds["
					+ threadholds + "]");
		}
		String[] array = threadholds.split(",");

		this.threadholds = new long[array.length];
		for (int i = 0; i < array.length; i++)
		{
			this.threadholds[i] = Long.parseLong(array[i].trim()) * 1000 * 1000;
		}

		if (this.threadholds.length > 0)
		{
			for (int i = 1; i < this.threadholds.length; i++)
			{
				if (this.threadholds[i - 1] >= this.threadholds[i])
				{
					throw new IllegalArgumentException("Illegal threadholds["
							+ threadholds + "]");
				}
			}
		}
	}

	public int getThreadholdLength()
	{
		return this.threadholds.length + 1;
	}

	public int getThreadholdIndex(long timeNano)
	{
		for (int i = 0; i < threadholds.length; i++)
		{
			if (timeNano < threadholds[i])
			{
				return i;
			}
		}

		return threadholds.length;
	}

	public long[] getThreadholdsInMS()
	{
		long[] threadholdsMS = new long[this.threadholds.length];
		for (int i = 0; i < threadholdsMS.length; i++)
		{
			threadholdsMS[i] = this.threadholds[i] / (1000 * 1000);
		}
		return threadholdsMS;
	}
}
